﻿/* Author:
 *  Andrea Vandin
 */
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TripLOAMobile.WS_SoapTrip;
using CompactFramework.Utilities.Localization;

using TripLOAMobile.InternalEntities;
using TripLOAMobile.InternalEntities.ExtensionsMethods;

namespace TripLOAMobile
{
    /// <summary>
    /// This class is used as root of the hierarchy of the forms that show a list of travel
    /// </summary>
    public partial class ShowListMinimalTravel : LocalizedForm
    {
        protected AnimationControl animation;

        /// <summary>
        /// Initializes a new instance of the <see cref="ShowListMinimalTravel"/> class.
        /// </summary>
        public ShowListMinimalTravel()
        {
            InitializeComponent();
            this.commonConstructorCode();
            this.AddMenusToDictionary();
            #region creation of the control for the animation
            animation = new AnimationControl();
            int x = this.Width / 2 - animation.Width / 2;
            int y = this.Height / 2 - animation.Height / 2;
            animation.Location = new Point(x, y);
            this.Controls.Add(animation);
            animation.BringToFront();
            #endregion
        }


        /// <summary>
        /// The subclasses of the LocalizedForm should override this method and use this method to
        /// associate all menu items with custom names with the help of <see cref="AddMenuToDictionary"/>
        /// method.
        /// </summary>
        protected override void AddMenusToDictionary()
        {
            this.AddMenuToDictionary(this.menuLeft, "menuLeft");
            this.AddMenuToDictionary(this.menuRight, "menuRight");
            this.AddMenuToDictionary(this.menuItem1, "menuBack");
            this.AddMenuToDictionary(this.menuItem2, "menuHome");
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="ShowListMinimalTravel"/> class.
        /// </summary>
        /// <param name="listOfMinimalTravel">The list of minimal travel.</param>
        /// <param name="listStatus">The list status.</param>
        internal ShowListMinimalTravel(TripInfo[] listOfMinimalTravel, string listStatus)
        {
            InitializeComponent();
            this.commonConstructorCode();

            if (listOfMinimalTravel != null)
            {
                ListOfMinimalTravel = listOfMinimalTravel;
                if (listOfMinimalTravel.Length < 6)
                    this.menuRight.Enabled = false;
                this.AddMenusToDictionary();
            }
        }

        #region NextResponseHandling
        /// <summary>
        /// Method for requesting a list of successive results. The request is asynchronuos.
        /// </summary>
        protected virtual void GetNextTravels() { }

        /// <summary>
        /// Method invoked after the reception of a requested list of travel
        /// </summary>
        /// <param name="e">The <see cref="TripLOAMobile.RemoteResourceProvider.TripListResponseEventArgs"/> instance containing the event data.</param>
        public virtual void rrp_NextResponse(RemoteResourceProvider.TripListResponseEventArgs e)
        {
            stopAnimation();

            listStatus = e.Status;
            if (e.Status == RemoteResourceProvider.okStatus)
            {
                if (InvokeRequired)
                    this.Invoke(new UpdateListHandler(UpdateList), new List<TripInfo>(e.Results));
                else
                    ListOfMinimalTravel = e.Results;

                if ((e.Results != null) && (e.Results.Length < this.listViewTravel.MaxTravelsForPage))
                {
                    this.menuRight.Enabled = false;
                }
            }
            else
            {
                MessageBox.Show(e.Status);
            }
        }
        #endregion

        /// <summary>
        /// Sets the list of minimal travel.
        /// Load the list in the control ListViewTravelControl that shows the list of travel
        /// </summary>
        /// <value>The list of minimal travel.</value>
        internal TripInfo[] ListOfMinimalTravel
        {
            set
            {
               
                if ((value == null))
                {
                    this.menuRight.Enabled = false;
                }
                else
                {
                    string[] linksTravel = new string[value.Length];
                    string[] commentTravel = new string[value.Length];
                    int[] arrayOfId = new int[value.Length];
                    int i = 0;

                    foreach (TripInfo t in value)
                    {
                        linksTravel[i] = t.Departure.getCity() + " - " + t.Arrival.getCity();
                        commentTravel[i] = (t.DepartureDate == null) ? "" : t.DepartureDate.Value.ToString();
                        arrayOfId[i] = t.ID;
                        i++;    
                    }

                    this.listViewTravel.Section();
                    this.listViewTravel.MultiColorsControl(Color.FromArgb(222, 243, 123), Color.White, Color.GreenYellow);
                    this.listViewTravel.Load(linksTravel, commentTravel, arrayOfId);
                    
  
                    if (value.Length == 0 || (this.listViewTravel.NowIsInTheLastPage() && this.listViewTravel.PageIsFull()))
                    {
                        this.menuRight.Enabled = false;
                    }
                    this.listViewTravel.GoNext();
                    if (value.Length == 0)
                    {
                        MessageBox.Show(ResourceManager.Instance.GetString("EmptyElementList"));
                        this.listViewTravel.GoNext();
                    }
                }
            }
        }

        #region ResponseHandling
        protected delegate void UpdateListHandler(List<TripInfo> list);
        protected void UpdateList(List<TripInfo> list)
        {
            ListOfMinimalTravel = list.ToArray();
        }
        #endregion


        protected string listStatus;


        /// <summary>
        /// Common constructor code.
        /// </summary>
        private void commonConstructorCode()
        {
            this.listViewTravel.TravelsAreBookable = this.travelsAreBookable();
        }


        /// <summary>
        /// Method that determines if this particular class contains travels bookable
        /// </summary>
        /// <returns></returns>
        protected virtual bool travelsAreBookable()
        {
            return true;
        }


        /// <summary>
        /// Handles the Click event of the menuItemBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void menuItemBack_Click(object sender, EventArgs e)
        {
            this.menuRight.Enabled = true;
            if (listViewTravel.ActualNumberPage <= 1)
                Welcome.formBuffer.Close();
            else
            {
                if (listViewTravel.ActualNumberPage == 2)
                    this.menuLeft.Text = "Back";
                this.listViewTravel.GoBack();

            }
            
        }


        /// <summary>
        /// Handles the Click event of the menuItemNext control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void menuItemNext_Click(object sender, EventArgs e)
        {

            if (listViewTravel.ActualNumberPage == 1 && this.listViewTravel.TravelsInPage())
            {
                this.menuItem1.Text = ResourceManager.Instance.GetString("ShowPrev");
                menuItem2.Enabled = true;
            }

            if (this.listViewTravel.NowIsInTheLastPage())
            {

                animation.StartAnimation();
                this.GetNextTravels();

            }
            else
            {
                this.listViewTravel.GoNext();
                if (this.listViewTravel.NowIsInTheLastPage())
                    this.menuRight.Enabled = false;

            }
        }
     

        #region start/stop animation
        protected delegate void animationDel();

        /// <summary>
        /// Stops the animation.
        /// </summary>
        protected void stopAnimation()
        {
            foreach (Control c in this.Controls)
            {
                c.Enabled = true;
            }
            this.menuLeft.Enabled = true;
            this.menuRight.Enabled = true;
            this.animation.StopAnimation();
        }

        /// <summary>
        /// Starts the animation.
        /// </summary>
        protected void startAnimation()
        {
            foreach (Control c in this.Controls)
            {
                c.Enabled = false;
            }
            this.menuLeft.Enabled = false;
            this.menuRight.Enabled = false;
            this.animation.StartAnimation();
        }
        #endregion

        /// <summary>
        /// Handles the Click event of the menuItem1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.menuRight.Enabled = true;
            if (listViewTravel.ActualNumberPage <= 1)
                Welcome.formBuffer.Close();
       
            else
            {
                if (listViewTravel.ActualNumberPage == 2)
                {
                    this.menuItem1.Text = ResourceManager.Instance.GetString("ShowListMinimalTravel_menuLeft");
                    menuItem2.Enabled = false;
                }
                this.listViewTravel.GoBack();
            }
        }

        /// <summary>
        /// Handles the Click event of the menuItem2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void menuItem2_Click(object sender, EventArgs e)
        {
            Welcome.formBuffer.Clear();
        }

        /// <summary>
        /// Handles the Load event of the ShowListMinimalTravel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ShowListMinimalTravel_Load(object sender, EventArgs e)
        {
            if (Welcome.formBuffer.GetFormNames().Contains("SearchTravel"))
                menuItem2.Enabled = true;
        }
    }
}
     

      
    

